home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7825 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.3 KB

  1. Path: solon.com!not-for-mail
  2. From: seebs@solutions.solon.com (Peter Seebach)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: What is &Variable (declared as: char Variable[10])?
  5. Date: 28 Feb 1996 20:44:17 -0600
  6. Organization: Usenet Fact Police (Undercover)
  7. Message-ID: <4h33u1$31f@solutions.solon.com>
  8. References: <4gqpa1$3h9@alcor.usc.edu> <4gtab6$acb@ceylon.gte.com> <313318b8.53776146@nntp.ix.netcom.com> <4h1u9d$sqq@ceylon.gte.com>
  9. NNTP-Posting-Host: solutions.solon.com
  10.  
  11. In article <4h1u9d$sqq@ceylon.gte.com>, Brenda  <g051286> wrote:
  12. >What is the definition of a pointer?  I have always been taught that a
  13. >pointer is simply an address in memory, and an array name (i.e. myarray)
  14. >is simply a CONSTANT address.
  15.  
  16. You have been taught lies.
  17.  
  18. An array name is merely an identifier; it has no meaning or value outside
  19. of the context of a program.
  20.  
  21. "myarray" is a sequence of 7 characters inside quotes.
  22.  
  23. When you get into a program using that name, you must look at the declaration
  24. *and the context* to learn what that name refers to.
  25.  
  26. Consider
  27.     int myarray[10], *myptr;
  28.  
  29. With these definitions
  30.     sizeof(myarray) == (10 * sizeof(int))
  31. but
  32.     sizeof(myptr) == (sizeof(int *))
  33.  
  34. ... They are only the same on the rare machine where (int *) is 10 times as
  35. large as int.  :)
  36.  
  37. >Of course there are differences between arrays and pointers
  38. >due to the fact that an array is a CONSTANT address and a pointer is a
  39. >VARIABLE address.
  40.  
  41. No, they are due to the fact that the array "myarray" is an object consisting
  42. of 10 integers, and the pointer "myptr" is an object consisting of one
  43. pointer.
  44.  
  45. You are being seriously misled by the fact that "in an expression context,
  46. a reference to an array decays into a pointer to its first member".
  47. For instance,
  48.     strcpy(myarray, "foo");
  49. works by silently passing strcpy the address of myarray[0], rather than
  50. the actual array.  This is because arrays are not really full-fledged
  51. objects in C, in some ways.
  52.  
  53. >And the reason I said you shouldn't (note shouldn't
  54. >not couldn't) say &myarray is:
  55.  
  56. >==================================================
  57. >(from "A Book On C", Kelley & Pohl, pg 200)
  58. >Constructs not to be pointed at:
  59. >1. Do not point at constants. (&3)
  60. >2. Do not point at arrays; an array name is a constant. (int a[77]; &a)
  61. >3. Do not point at ordinary expressions &(k + 99)
  62. >4. Do not point at register variables. (register v; &v)
  63. >The address operator can be applied to variables and array elements.
  64. >===================================================
  65.  
  66. This is stupid.
  67. 1, 3, and 4 are all *illegal*.  You *cannot* do them.
  68. 2 is perfectly legal, and is exactly what you may want to do in some cases.
  69.  
  70. I am much less impressed with "A Book on C" than I used to be.
  71.  
  72. >So again I say, myarray is DEFINITELY a pointer (i.e. address in memory).
  73.  
  74. And you are wrong.  Please understand; the people you are arguing with
  75. mostly have 10+ years of experience and study, and have written
  76. hundreds of programs, libraries, and occasionally just modules.  We're
  77. not stupid.
  78.  
  79. &myarray is a pointer to an array of 10 ints.  myarray is an array of
  80. 10 ints.  In an expression, myarray is *treated like* a pointer to a
  81. single int.
  82.  
  83. -s
  84. -- 
  85. Peter Seebach - seebs@solon.com - Copyright 1996 Peter Seebach.
  86. C/Unix wizard -- C/Unix questions? Send mail for help.  No, really!
  87. FUCK the communications decency act.  Goddamned government.  [literally.]
  88. The *other* C FAQ - http://www.solon.com/~seebs/c/c-iaq.html
  89.